package com.jianglin.state2.controller;

import com.jianglin.state2.core.ResponsePack;
import com.jianglin.state2.dao.entity.Client;
import com.jianglin.state2.model.params.ClientAddParams;
import com.jianglin.state2.model.params.ClientUpdateParams;
import com.jianglin.state2.service.ClientService;
import com.jianglin.state2.util.SqlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponsePack
@RequestMapping("/client")
@Api(tags = {"客户信息"})
public class ClientController {

    private final ClientService clientService;

    @Autowired
    public ClientController(ClientService clientService){
        this.clientService = clientService;
    }

    @GetMapping("/all")
    @ApiOperation("获取所有客户信息")
    public List<Client> getAll() {
        return clientService.list();
    }

    @GetMapping("/count")
    @ApiOperation("获取客户数")
    public Integer getCount(){
        return clientService.count();
    }

    @GetMapping("/clientId/{clientId}")
    @ApiOperation("根据客户ID查找客户")
    public Client getEmployeeById(@ApiParam("客户ID") @PathVariable Integer clientId){
        return clientService.listByMap(SqlUtil.getColumnMap("clientId",clientId)).get(0);
    }

    @PostMapping("/add")
    @ApiOperation("添加客户")
    public boolean saveClient(@Validated @RequestBody ClientAddParams clientAddParams){
        Client client = clientAddParams.convertOut();
        return clientService.save(client);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新客户信息")
    public boolean updateClient(@ApiParam("客户编号") @PathVariable Integer id,
                                @Validated @RequestBody ClientUpdateParams clientUpdateParams){
        Client client = clientUpdateParams.convertOut();
        client.setClientId(id);
        return clientService.updateById(client);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除客户")
    public boolean deleteClient(@ApiParam("客户编号") @PathVariable Integer id){
        return clientService.removeById(id);
    }
}